package com.chhuang.core.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @ClassName IsExist
 * @Description 根据条件判断是否存在
 * @Author Darren Huang
 * @Date 2022/11/23 23:26
 * @Version 1.0
 */
public class Select1Limit1 extends AbstractMethod {

    public Select1Limit1() {
        super(ChSqlMethod.SELECT1_LIMIT1.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public Select1Limit1(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        ChSqlMethod sqlMethod = ChSqlMethod.SELECT1_LIMIT1;
        String sql = String.format(sqlMethod.getSql(), sqlFirst(), tableInfo.getTableName(),
                sqlWhereEntityWrapper(true, tableInfo), sqlComment());
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Integer.class);
    }

    public String getMethod(ChSqlMethod sqlMethod) {
        return StringUtils.isBlank(methodName) ? sqlMethod.getMethod() : this.methodName;
    }
}
